/**
 * 
 */
package no.hig.ekm.ircclient;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.WindowConstants;

import jerklib.Session;

/**
 * Generates GUI for and handles the loading of channel list
 * @author Kristofers
 * 
 */
public class ChannelList extends JInternalFrame {

	private static final long serialVersionUID = 1L;

	private static JButton btnSearch;
	private JList<String> chanList;
	private JLabel lblMax;
	private JPanel pnlControls;
	private JPanel pnlCount;
	private static JProgressBar progBar;
	private JScrollPane scrollPane;
	private static JSpinner spinner;
	private static JTextField txtName;
	// private AbstractListModel model;
	static DefaultListModel<String> model;
	private static String serverName = null;

	/**
	 * Default constructor
	 * @param selectedServer
	 */
	public ChannelList(String selectedServer) {
		super("List channels for " + selectedServer, true, true, false);
		initComponents();
		serverName = selectedServer;
	}

	/**
	 * Adds a channel to JList
	 * @param channel
	 */
	public static void addChan(String channel) {
		String s = txtName.getText();
		int c = (int) spinner.getValue();
		if (model.getSize() < c) {
			if (channel.contains(s))
				model.addElement(channel);
		} else {
			progBar.setIndeterminate(false);
			btnSearch.setText("Search");
			btnSearch.setToolTipText("Click to start");
			// TODO: STOP /list command
		}
	}

	/**
	 * Generates the main window with its components
	 */
	private void initComponents() {
		// java.awt.GridBagConstraints gridBagConstraints;

		scrollPane = new JScrollPane();
		chanList = new JList<String>();
		pnlControls = new JPanel();
		progBar = new JProgressBar();
		btnSearch = new JButton();
		txtName = new JTextField();
		pnlCount = new JPanel();
		lblMax = new JLabel();
		spinner = new JSpinner();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setMaximumSize(new java.awt.Dimension(480, 800));
		setMinimumSize(new java.awt.Dimension(180, 250));
		setPreferredSize(new java.awt.Dimension(240, 360));

		chanList.setModel(model = new DefaultListModel<String>());

		scrollPane.setViewportView(chanList);

		getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER);

		pnlControls.setLayout(new java.awt.BorderLayout());
		pnlControls.add(progBar, java.awt.BorderLayout.PAGE_START);

		btnSearch.setText("Search");
		btnSearch.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				String text = (String) evt.getActionCommand();
				//"Toggling button"
				Session s = Main.cm.getSession(serverName);
				if (s != null) {
					if (text.equals("Search")) {
						model.removeAllElements();
						progBar.setIndeterminate(true);
						btnSearch.setText("Searching...");
						btnSearch.setToolTipText("Click to stop");
						if (s != null) {
							s.chanList();
						}
					} else if (text.equals("Searching...")) {
						progBar.setIndeterminate(false);
						btnSearch.setText("Search");
						btnSearch.setToolTipText("Click to start");
						// TODO: STOP /list command
					}
				}
			}
		});
		pnlControls.add(btnSearch, java.awt.BorderLayout.SOUTH);

		txtName.setText("sol");
		txtName.setToolTipText("Enter the channel name you are looking for");
		pnlControls.add(txtName, java.awt.BorderLayout.CENTER);

		pnlCount.setLayout(new BoxLayout(pnlCount, BoxLayout.LINE_AXIS));

		lblMax.setText("Max:");
		lblMax.setToolTipText("");
		pnlCount.add(lblMax);

		spinner.setToolTipText("Set maximum relevant channels to list");
		spinner.setAutoscrolls(true);
		spinner.setValue(10);
		spinner.addChangeListener(new javax.swing.event.ChangeListener() {
			public void stateChanged(javax.swing.event.ChangeEvent evt) {
				//Do not allow negative values
				if ((int) spinner.getValue() < 1) {
					spinner.setValue((int) 1);
				}
			}
		});
		pnlCount.add(spinner);

		//Tries to connect to selected channel when double-clicked on it
		pnlControls.add(pnlCount, java.awt.BorderLayout.EAST);
		MouseListener mouseListener = new MouseAdapter() {
			public void mouseClicked(MouseEvent mouseEvent) {
				JList theList = (JList) mouseEvent.getSource();
				if (mouseEvent.getClickCount() == 2) {
					int index = theList.locationToIndex(mouseEvent.getPoint());
					if (index >= 0) {
						Object o = theList.getModel().getElementAt(index);
						System.out.println("Connecting to: " + o.toString());
						try {
							Main.cm.joinChannel(serverName, o.toString());
						} catch (IRCClientException e) {
						}
					}
				}
			}
		};
		chanList.addMouseListener(mouseListener);
		getContentPane().add(pnlControls, java.awt.BorderLayout.PAGE_END);
		pack();
		setVisible(true);
	}

}
